/**
 * 注册
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/5
 */
var self = id = '';
document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview();
	
	var phone = '';
	
	id = self.aid;
	
	id ? $('.title').text('完善资料') : $('.title').text('注册');
	
	//获取验证码
	$('.verification').on('click' , function(){
		
		phone = $('input[name=phone]').val();
		
		if(!p1.test(phone)){
			
			toast('手机号码格式有误'); return;
			
		}
		
		id ? getverify(phone) : checkUser(phone); 
		
	})
	
	
	
	$('.register').on('click' , function(){
		
		var pass = $('input[name=pass]').val() , verify = $('input[name=verification]').val();
		
		if(!phone) return;
		
		if(!pass.trim()){
			
			toast('密码不能为空'); return;
			
		}
		
		if(!verify.trim()){
			
			toast('验证码不能为空'); return;
			
		}
		
		var verify_local = plus.storage.getItem('verify');
		
		if(!verify_local || verify_local != verify){
			
			toast('验证码输入有误');return;
			
		}

		plus.nativeUI.showWaiting('注册中...');
		
		$.ajax({
			
			url : apiRoot + '/Home/Member/register/' , 
			
			type : 'post' , 
			
			data : {
				
				aid : id,
				
				phone : phone , 
				
				pass : pass,
				
				yzm:verify
				
			} , 
			
			success : function(data){
				
				plus.nativeUI.closeWaiting();
				
				plus.storage.removeItem('verify');
				console.log(JSON.stringify(data));
				if(data){
					
					if(id){
						
						toast('登录成功');
					
						//保存用户数据
						plus.storage.setItem('memberInfo' , data + '')
					
						openNewpage('index.html');
						
					}else{
						
						toast('注册成功');
						
						mui.fire(plus.webview.getWebviewById(self.self_window) , 'register' ,{
							
							'phone' : phone 
							
						})
						
						mui.back();
					}
					
				}
				
			} , 
			error : function(e){
				
				plus.nativeUI.closeWaiting();
				
				toast('连接超时 ，请检查网络');
				
			}
		})
	})
	
}
